﻿using STHPMS1.Common.C_Entity;
using STHPMS1.Common.C_Entity.Stmpms1;
using STHPMS1.ICore;
using STHPMS1.ICore.I_Core;
using STHPMS1.ICore.I_DAL;
using STHPMS1.ICore.I_Entity.Stmpms1;
using STHPMS1.ICore.I_Serv;
using STHPMS1.ICore.I_UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace STHPMS1.Web.W_Controller.Decision.Fondos
{
    [RoutePrefix("api/dffunlimproj")]
    public class DF_FunlimprojController : STHPMS1.Web.W_Core.MyController 
    {
        [Route("")]
        [Route("page/{page?}", Name = "GetdffunlimprojByPage")]
        [Route("rows/{rows?}")]
        [Route("page/{page?}/rows/{rows?}")]
        public IHttpActionResult Get(string anni_name = null, string analy_year = null, int page = 1, int rows = 10)
        {
            int p = page;
            int s = rows;

            PagingInfo info = new PagingInfo();
            info.PageIndex = p > 0 ? p - 1 : 0;
            info.PageSize = s;
            info.TotalRecords = 0;

            Expression<Func<Df_Funlim_Proj, bool>> exp = null;
            if (anni_name != null && anni_name.ToString().Length > 0)
                exp = pre => pre.Anni_Name == anni_name.ToString();
            if (anni_name != null && anni_name.ToString().Length > 0 && analy_year != null && analy_year.ToString().Length > 0)
                exp = pre => pre.Anni_Name == anni_name.ToString() && pre.Analy_Year == analy_year.ToString();


            var data = this.Scene.Bll.Df_Funlim_Proj.Get<Df_Funlim_Proj>(exp, info).ToArray();
            if (data == null) return NotFound();

            var result = new { rows = data, total = info.TotalRecords };

            return Ok(result);
        }

        /// <summary>
        /// 同步数据 资金受限条件养护方案
        /// </summary>
        /// <param name="anniname"></param>
        [Route("SetInsert")]
        [HttpGet]
        public void SetInsert(string anniname,string analy_year)
        {

            this.Scene.Bll.Df_Funlim_Proj.Insert(anniname, analy_year);

            #region 转移到BLL层
            //IKeyGenrateService serv = MyApp.Locator.GetInstance<IKeyGenrateService>();
            //ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
            //dao.Delete<Df_Funlim_Proj>(p => p.Anni_Name == anniname && p.Analy_Year == analy_year);  //资金模型名称

            //Expression<Func<Df_Costmod_Para, bool>> exp = null;
            //if (anniname != null && anniname.ToString().Length > 0 && analy_year != null && analy_year.ToString().Length > 0)
            //    exp = pre => pre.Anni_Name == anniname.ToString() && pre.Analy_Year == analy_year;

            //List<Df_Costmod_Para> data = this.Scene.Bll.Df_Costmod_Para.Get<Df_Costmod_Para>(exp, null).ToList();  //资金模型参数   理论上只有一条数据
            //int i = 0;
            //foreach (Df_Costmod_Para info in data)
            //{
            //    List<Dn_Divi_Planning> list = dao.Df_Main_Cost.GetDivi<Dn_Divi_Planning>(info.Roadnet_Name,info.Analy_Year);

            //    foreach (Dn_Divi_Planning diviinfo in list)
            //    {

            //        Df_Funlim_Proj funinfo = new Df_Funlim_Proj();
            //        funinfo.Id = serv.NewGuid();
            //        funinfo.Anni_Name = anniname;
            //        funinfo.Roadnet_Name = info.Roadnet_Name;
            //        funinfo.Rdwayid = diviinfo.Rdwayid;
            //        funinfo.Mainsegid = diviinfo.Mainsegid;
            //        funinfo.Beginmp = diviinfo.Beginmp;
            //        funinfo.Endmp = diviinfo.Endmp;

            //        //funinfo.Rd_Class = diviinfo.Rdclass_Dictid;
            //        funinfo.Rd_Class = dao.Dp_Dailymain_Cost.GetB_Invntory(diviinfo.Mainsegid).Rdclass_Dictid;
                    
            //        funinfo.Analy_Year = info.Analy_Year;
            //        funinfo.Segid_Main = diviinfo.Segid_Main;
            //        funinfo.Carry_Year = info.Analy_Year;

            //        Random ro = new Random();
                    
                    
            //        //funinfo.Pci = ro.Next(5, 100);  //??
            //        //funinfo.Pqi = ro.Next(5, 100);  //？？

            //        funinfo.Pci = dao.Df_Funlim_Proj.GetDn_Divi_Planning(info.Analy_Year, diviinfo.Mainsegid).Pci;
            //        funinfo.Pqi = dao.Df_Funlim_Proj.GetDn_Divi_Planning(info.Analy_Year, diviinfo.Mainsegid).Pqi;


            //        //该路段为大中修养护，当年养护后，以后每年则进行日常性养护
            //        if ((i >= 1 && diviinfo.Segid_Main == 12002) || (i >= 1 && diviinfo.Segid_Main == 12003))
            //        {
            //            funinfo.Segid_Main = 12000;
            //        }

            //        if (diviinfo.Segid_Main == 12000) //该路段为日常养护
            //        {
            //            funinfo.Segid_Main = 12000;
            //        }
            //        if (diviinfo.Segid_Main == 12001)   //该路段为预防性养护，判断是否单年养护，如果不是则进行日常性养护
            //        {
            //            string mainyear = dao.Dp_Preven_Detail.GetMainYear(diviinfo.Mainsegid);
            //            if (info.Analy_Year == mainyear)
            //            {
            //                funinfo.Segid_Main = 12001;
            //            }
            //            else
            //            {
            //                funinfo.Segid_Main = 12000;
            //            }
            //        }

            //        if ((i == 0 && diviinfo.Segid_Main == 12002) || (i == 0 && diviinfo.Segid_Main == 12003))  //该路段为大中修
            //        {
            //            funinfo.Segid_Main = diviinfo.Segid_Main;
            //        }


            //        //计算费用

            //        if (funinfo.Segid_Main == 12000)
            //        {
            //            double lenght = Convert.ToDouble(diviinfo.Endmp - diviinfo.Beginmp);

            //            //通过决策路段获取基础路段信息
            //            IB_Invntory b_invntoryInfo = this.Scene.Bll.Dp_Dailymain_Cost.GetB_Invntory(diviinfo.Mainsegid);
            //            int Year = (DateTime.Now - Convert.ToDateTime(b_invntoryInfo.Begindate)).Days / 365;

            //            double cost = dao.Df_Dailymian_Cost.GetDaily_Unitcost(Convert.ToInt32(b_invntoryInfo.Pvmttype_Dictid), b_invntoryInfo.Rdclass_Dictid.ToString(), Year);  //通车年限？？？
            //            funinfo.Segid_Maincost = lenght / 1000 * cost;
            //        }
            //        if (funinfo.Segid_Main == 12001)
            //        {
            //            funinfo.Segid_Maincost = dao.Dp_Preven_Detail.GetCost(diviinfo.Mainsegid);
            //        }
            //        if (funinfo.Segid_Main == 12002)
            //        {
            //            funinfo.Segid_Maincost = 10;   //??
            //        }
            //        if (funinfo.Segid_Main == 12003)
            //        {
            //            funinfo.Segid_Maincost = 100;  //??
            //        }
            //        dao.Save<Df_Funlim_Proj>(funinfo);
            //    }

            //    i++;
            //}
            #endregion


        }

        //int i = 1;
        

        /// <summary>
        /// 资金安排 资金受限条件养护方案
        /// </summary>
        /// <param name="anniname"></param>
        [Route("SetEditYear")]
        public void GetEditYear(string anniname, string analy_year)
        {
            this.Scene.Bll.Df_Funlim_Proj.GetEditYear(anniname, analy_year);

            #region 转移到BLL层
            //ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
            //Expression<Func<Df_Costmod_Para, bool>> exp = null;
            //if (anniname != null && anniname.ToString().Length > 0)
            //    exp = pre => pre.Anni_Name == anniname.ToString() && pre.Analy_Year == analy_year.ToString();

            //List<Df_Costmod_Para> data = this.Scene.Bll.Df_Costmod_Para.Get<Df_Costmod_Para>(exp, null).ToList();  //资金模型参数 理论上只有一条数据

            //foreach (Df_Costmod_Para info in data)
            //{

            //    double annu_fundlim = Convert.ToDouble(info.Annu_Fundlim); //资金限额/万元

            //    Expression<Func<Df_Full_Order, bool>> exp1 = null;
            //    List<Df_Full_Order> data1 = this.Scene.Bll.Df_Full_Order.Get<Df_Full_Order>(exp1, null).OrderBy(p => p.Ordernum).ToList();  //各级公路资金分配表

            //    foreach (Df_Full_Order info1 in data1)
            //    {
            //        GetS(dao,info.Analy_Year, anniname, Convert.ToInt32(info1.Rd_Class), Convert.ToInt32(info1.Max_Pci), Convert.ToInt32(info1.Max_Pqi),ref annu_fundlim);
            //    }
            //}

            #endregion

           
        }

        #region 转移到BLL层 
        //public void GetS(ISthpms1Dao dao, string year, string anniname, int rd_class, int pci, int pqi, ref double annu_fundlim)
        //{
        //    IDf_Funlim_Proj funinfo = dao.Df_Funlim_Proj.GetDf_Funlim_Proj(anniname, rd_class, pci, pqi);
        //    if(funinfo!=null)
        //    {
        //        if(annu_fundlim>=funinfo.Segid_Maincost)  //资金限额大于等于路段养护费用
        //        {
        //            dao.Update<Df_Funlim_Proj>(new { Ordernum = i, Cost_Year = year }, p => p.Id == funinfo.Id);
        //            annu_fundlim = annu_fundlim - Convert.ToDouble(funinfo.Segid_Maincost);
        //            i++;
        //            GetS(dao,year, anniname, rd_class, pci, pqi,ref annu_fundlim);
        //        }
        //    }
        //}
        #endregion


    }
}